package cn.hhchat.wp.service;

import cn.hhchat.wp.entity.ConfigEntity;
import cn.hhchat.wp.enums.ConfigEnum;
import cn.hhchat.wp.repository.ConfigRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created this one by HMH on 2017/10/27.
 */
@Service
public class ConfigService {

    public ConfigEntity switchReg(boolean isAllow) {
        ConfigEntity config = configRepository.findByName(ConfigEnum.allowReg.name());
        config.setBoolValue(isAllow);
        return save(config);
    }

    public ConfigEntity switchLogin(Boolean isAllow) {
        ConfigEntity config = configRepository.findByName(ConfigEnum.allowLogin.name());
        config.setBoolValue(isAllow);
        return save(config);
    }

    public ConfigEntity get(String name) {
        return configRepository.findByName(name);
    }

    public ConfigEntity save(ConfigEntity config) {
        return configRepository.save(config);
    }

    public List<ConfigEntity> getAll() {
        return configRepository.findAllByIsActive(true);
    }

    @Autowired
    private ConfigRepository configRepository;
}
